<?php
/**
 * lsys orm
 * @author     Lonely <shan.liu@msn.com>
 * @copyright  (c) 2017 Lonely <shan.liu@msn.com>
 * @license    http://www.apache.org/licenses/LICENSE-2.0
 */
namespace LSYS\ORM;


class EntityActive extends Entity {
	protected $_object=array();
	/**
	 * @param ORM $orm
	 */
	public function __construct(Active $orm) {
		parent::__construct($orm);
		if(!isset($this->_object[$this->_orm->primary_key()])) $this->_object=$orm->table_defaults();
	}
	/**
	 * {@inheritDoc}
	 * @see \LSYS\ORM\Entity::as_array()
	 */
	public function as_array() {
		return $this->_object;
	}
	/**
	 * @param string $column
	 * @param string $value
	 * @throws Exception
	 * @return \LSYS\ORM\EntityActive
	 */
	public function __set($column,$value){
		if ($this->_orm===null||array_key_exists($column, $this->_object))
		{
			$this->_object[$column] = $value;
		}
		else
		{
			throw new Exception(
				__("The :column property does not exist in the :entity entity",
				array("column"=>$column,"entity"=>__CLASS__))
			);
		}
		return $this;
	}
	/**
	 * @param string $column
	 * @return boolean
	 */
	public function __isset($column){
		return array_key_exists($column, $this->_object);
	}
	/**
	 * @param string $column
	 * @throws Exception
	 * @return mixed
	 */
	public function __get($column)
	{
		if (array_key_exists($column, $this->_object))
		{
			return $this->_object[$column];
		}
		else
		{
			throw new Exception(
				__("The :column property does not exist in the :entity entity",
				array("column"=>$column,"entity"=>__CLASS__))
			);
		}
	}
	/**
	 * Returns the value of the primary
	 *
	 * @return mixed Primary
	 */
	public function pk()
	{
		$primary_key=$this->_orm->primary_key();
		if (!isset($this->_object[$primary_key])) return null;
		return $this->_object[$primary_key];
	}
	/**
	 * Allows serialization of only the object data and state, to prevent
	 * "stale" objects being unserialized, which also requires less memory.
	 *
	 * @return string
	 */
	public function serialize() {
		$cls=get_class($this->_orm);
		$data=array("cls"=>$cls,'table_name'=>$this->_orm->table_name(),'object'=>$this->_object);
		return serialize($data);
	}
	/**
	 * Prepares the database connection and reloads the object.
	 *
	 * @param string $data
	 *        	String for unserialization
	 * @return void
	 */
	public function unserialize($data) {
		$data=unserialize($data);
		$cls=isset($data['cls'])?$data['cls']:null;
		switch ($cls){
			case 'LSYS\ORM\Active':
				$tablename=isset($data['table_name'])?$data['table_name']:null;
				$this->_orm=new Active($tablename);
			break;
			default: $this->_orm=$this->_init_orm();
		}
		$this->_object=isset($data['object'])?$data['object']:$this->_orm->table_defaults();
	}
	/**
	 * clear entity
	 * @return \LSYS\ORM\EntityActive
	 */
	public function clear(){
		$this->_object=$this->_orm->table_defaults();
		$this->_orm->reset();
		return $this;
	}
	/**
	 * compare Entity
	 * @param Entity $entity
	 * @param array $field
	 * @return boolean
	 */
	public function diff(\LSYS\Database\Utils\Entity $entity,$field=null){
		if(is_string($field)) $field=array($field);
		$columns=$this->_orm->table_columns();
		if (is_array($field)) $columns= array_intersect($columns, $field);
		$diff=array();
		foreach ($columns as $v){
			if ($this->_object[$v]!==$entity->{$v}){
				$diff[]=$v;
			}
		}
		return $diff;
	}
	/**
	 * dump object
	 */
	public function __debugInfo(){
		$out=array(
			'object'=>$this->_object,
			'columns'=>$this->_orm->table_defaults(),
			'primary_key'=>$this->_orm->primary_key(),
		);
		return $out;
	}
}